﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;
using System.Windows.Input;

namespace FruitManagerApp
{
    public class CityViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private CityModel gCityModel;

        public CityViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gCityModel = new CityModel();

            UpdateList();
            UpdateUI();
        }

        public void UpdateUI()
        {

        }

        public void UpdateList()
        {
            DataList.Clear();
            HttpResponse response = API.SelectCity(0);
            if(response.S== HttpResponse.Success)
            {
                if(response.D != "")
                {
                    List<CCity> list = JSON.parse<List<CCity>>(response.D);
                    foreach (var item in list)
                    {
                        DataList.Add(item);
                    }
                }
            }

            Province.Clear();
            response = API.SelectProvince();
            if (response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }
            Province.Add(new CProvince(0, "全部"));
            ProvinceUID = ProvinceUID;
        }

        public ObservableCollection<CCity> DataList
        {
            get
            {
                return gCityModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CProvince> province = new ObservableCollection<CProvince>();
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        #region Command
        protected override void AddCommand(object obj)
        {
            CityAdd window = new CityAdd();
            CityAddViewModel viewModel = new CityAddViewModel(window, this);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected override void DeleteCommand(object obj)
        {
            List<int> list = new List<int>();
            foreach (var item in DataList)
            {
                if (item.IsChoose)
                {
                    list.Add(item.UID);
                }
            }

            if (list.Count != 0)
            {
                API.DeleteCity(list);
                UpdateList();
            }
        }

        protected override void CheckedCommand(object obj)
        {
            if(IsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void ItemEditClick(object obj)
        {
            CityEdit window = new CityEdit();
            CityEditViewModel viewModel = new CityEditViewModel(window, this, (CCity)obj);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected DelegateCommand<object> _provinceChanged;
        public DelegateCommand<object> _ProvinceChanged
        {
            get
            {
                return _provinceChanged ?? (_provinceChanged = new DelegateCommand<object>(ProvinceChanged));
            }
        }

        protected virtual void ProvinceChanged(object obj)
        {
            DataList.Clear();
            HttpResponse response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                if (response.D != "")
                {
                    List<CCity> list = JSON.parse<List<CCity>>(response.D);
                    foreach (var item in list)
                    {
                        DataList.Add(item);
                    }
                }
            }
        }
        #endregion
    }
}
